package com.business.system.service.impl;

import com.business.system.bean.*;
import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.service.Service;

import com.business.system.service.BackFeeLogService;
import com.business.system.util.BackFeeUtil;
import com.business.system.util.DBTools;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author: xudy
 * @date: 2018/04/25 14:00
 * @description:
 */
@org.springframework.stereotype.Service
public class BackFeeLogServiceImpl implements BackFeeLogService {
    @Override
    public boolean insertApplyFeeLog(Service service, ApplyBean applyBean, WxUserBean getFeeUser) throws Exception {
        BackFeeLogBean backFeeLogBean = new BackFeeLogBean();
        backFeeLogBean.setId(KeyGenerator.getSystemUUID());
        backFeeLogBean.setUserId(getFeeUser.getId());
        backFeeLogBean.setFromUserId(applyBean.getUserId());
        backFeeLogBean.setResourceId(applyBean.getId());
        backFeeLogBean.setType(BackFeeLogBean.RECM);
        backFeeLogBean.setState(BackFeeLogBean.TO_CREATE_SETTLE);
        //计算佣金率
        double rate = 0;
        StringBuilder sql = new StringBuilder();
        //获取该申请对应的全部品牌
        sql.append("select t1.* from USER_BRAND t1 left join BRAND t2 on t1.brand_id=t2.id where t1.apply_id=?");
        double totalBackFee = 0;
        DecimalFormat format = new DecimalFormat("#.00");
        List<BrandBean> brands = DBTools.getBeanList(service,BrandBean.class,sql.toString(),applyBean.getId());
        double[] prate = new double[brands.size()];
        for(BrandBean bean : brands){
            if(applyBean.getType()==1){
                rate+=bean.getAgentRate();
            }else{
                rate+=bean.getFranchiseRate();
            }
        }
        for(int i=0;i<brands.size();i++){
            if(applyBean.getType()==1){
                prate[i] = brands.get(i).getAgentRate()/rate;
            }else{
                prate[i] = brands.get(i).getFranchiseRate()/rate;
            }
        }
        for(int i=0;i<brands.size();i++){
            BrandBean bean = brands.get(i);
            if(applyBean.getType()==1){
                if(bean.getAgentRate()==0){
                    continue;
                }
                totalBackFee+=BackFeeUtil.calcBackFee(bean.getAgentFee(),Double.parseDouble(format.format(bean.getAgentRate()/100)),applyBean.getRealFee()*prate[i]);
            }else{
                if(bean.getFranchiseRate()==0){
                    continue;
                }
                totalBackFee+=BackFeeUtil.calcBackFee(bean.getFranchiseFee(),Double.parseDouble(format.format(bean.getFranchiseFee()/100)),applyBean.getRealFee()*prate[i]);
            }
        }
        backFeeLogBean.setBackFee(totalBackFee);
        int i = DBTools.insertBean(service, backFeeLogBean);
        return i>0;
    }

    @Override
    public boolean insertRecmFeeLog(Service service, GoodsOrdersBean orders, WxUserBean getFeeUser) throws Exception {
        BackFeeLogBean backFeeLogBean = new BackFeeLogBean();
        backFeeLogBean.setId(KeyGenerator.getSystemUUID());
        backFeeLogBean.setUserId(getFeeUser.getId());
        backFeeLogBean.setFromUserId(orders.getUserId());
        backFeeLogBean.setResourceId(orders.getId());
        backFeeLogBean.setType(BackFeeLogBean.AGENT);
        backFeeLogBean.setState(BackFeeLogBean.TO_CREATE_SETTLE);
        //1.获取订单商品的全部品牌
        StringBuilder sql = new StringBuilder();
        sql.append("select t3.* from order_goods t1 left join goods t2 on t1.GOODS_ID = t2.id left join brand t3 on t2.BRAND_ID=t3.id where t1.orders_id=?");
        //2.获取订单全部商品
        List<BrandBean> brands = DBTools.getBeanList(service,BrandBean.class,sql.toString(),orders.getId());
        sql = new StringBuilder();
        sql.append("select t1.* from order_goods t1 left join goods t2 on t1.GOODS_ID = t2.id left join brand t3 on t2.BRAND_ID=t3.id where t1.orders_id=?");
        List<OrderGoodsBean> orderGoods = DBTools.getBeanList(service,OrderGoodsBean.class,sql.toString(),orders.getId());
        double totalBackFee = 0;
        DecimalFormat format = new DecimalFormat("#.00");
        for(int i =0;i<orderGoods.size();i++){
            OrderGoodsBean goods = orderGoods.get(i);
            BrandBean brand = brands.get(i);
            totalBackFee+=BackFeeUtil.calcBackFee(goods.getSalePrice()*goods.getCounts(),Double.parseDouble(format.format(brand.getAgentRate())),goods.getSalePrice()*goods.getCounts());
        }
        backFeeLogBean.setBackFee(totalBackFee);
        int i = DBTools.insertBean(service, backFeeLogBean);
        return i>0;
    }
}